<?php

/**
 * @author
 * @copyright 2014
 */
class Smsemail extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model();
        $this->load->model(array('department_model'));
        //$this->lang->load('title');
    }
    
    function _remap($method, $params = array()) {
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $params);
        }
        $this->_view();
    }
    
    function _view() {
		$data = new stdClass();
        $this->load->helper('form');
        $data->page = 0;
        $title = "Trung tâm"; 
		$data->cotrol = $this->uri->segment(1);
		$right = $this->login_model->permission($data->cotrol);		
		if(!isset($right['view'])){
			redirect(base_url().'home');
		}
		$data->rights = $right;
        $content = $this->load->view('view', $data, true);
		$this->pub->write('title',$title,true);
		$this->pub->write('content',$content,true);
        $this->pub->render();
    }
    
	function getList(){
		 $data = new stdClass();
		 $result = new stdClass();
		 $page=$this->input->post('page');
         $counts=$this->input->post('count');
		 if(empty($counts)){
			$counts = $this->pub->config['numrows'];
		 }
		 $search = json_decode($this->input->post('search'));
         $controller = $this->uri->segment(1);
		 $right = $this->login_model->permission($controller); 
		 $data->rights = $right;	
		 $dept = $this->model->where('thungrac',0);
		 //if(!empty($search->name)){
			//$dept = $dept->like('names',$search->name);
		 //}
		 $count    = $dept->count_all(false); 
         if($counts!='Tất cả'){
           $dept = $dept->limit($counts,$page);
		 }
         else{
            $counts = $count;
		 }
         $dept = $dept->order_by('id','ASC')->find_all(false);
		 $page_view='&nbsp;';
        
		 if($count>0){
			$page_view=$this->pub->phantrang($count,$counts,5,'smsemail/',$page);
		 }
         if(empty($page_view)) $page_view='&nbsp;';
         $result->paging = $page_view;
         $result->cPage = $page;
		 $data->datas = $dept;
		 $data->count = $count;
         $result->content = $this->load->view('list',$data,true);
         $result->action = $this->pub->action;
		 echo json_encode($result);
	}
    
    function edit(){
        $this->insert();
    }
    
	function insert(){	   
		$login = $this->pub->GetSession('login');
		if(empty($login['taikhoan'])){
			redirect(base_url().'authorize');
		}
		$this->load->helper('form');
		$data = new stdClass();		
		//$id = $this->input->post('id');
        $last = $this->uri->total_segments();
		$id = $this->uri->segment($last);        
		$query = $this->model->where('id',$id)->find();
        //print_r($query); exit();
		$data->datas = $query;
		$data->id = $id;
        
        $arr_donvi_soap = $this->model->arr_donvi_soap($login);
        //$phongbans = array();
        //$trungtams = array();
		$donvis = array();
		for($i=0; $i<count($arr_donvi_soap); $i++) {
            /*if($arr_donvi_soap[$i]['loai']==1) {            
            $phongbans[$arr_donvi_soap[$i]['id']] = $arr_donvi_soap[$i]['ten'];
            } elseif($arr_donvi_soap[$i]['loai']==2) {
                $trungtams[$arr_donvi_soap[$i]['id']] = $arr_donvi_soap[$i]['ten'];
            }
            */
            $donvis[$arr_donvi_soap[$i]['id']] = $arr_donvi_soap[$i]['ten'];
		}         
        $data->donvis = $donvis;				
        $data->nvchuyenquans = $this->model->combo_chuyenquan($login);        
                
		$data->titles = "Thêm mới kết luận";
		$content = $this->load->view('form', $data, true);
		$this->pub->write('content',$content,true);
		$this->pub->render();	
	}
    
	function save(){
		$str = $this->input->post('str');
		if($str !='{}'){
			$json = json_decode($str);
			$data = array();
			foreach($json as $clm=>$val){
				$data[$clm] = $val;
			}
            //print_r($data); exit();
			$result = $this->model->save($data);
			echo $result;
		}
		else{
			echo 0;	
		}
	}
    
	function delete(){
		$str = $this->input->post('str');
		$item = $this->input->post('item');
		$this->model->delete($str,$item);
	}
	function loadSearch(){
		$key = $this->input->post('key');
		$cartype = $this->model->where('thungrac',0)
							   ->select('typename')	
							   ->like('typename',$key) 
							   ->limit(10)
							   ->find_all();
		$str = "";
		foreach($cartype as $item){
			$str.= ','.($item->typename).'';
		}
		$str = substr($str,1);
		echo $str; 
	}
}